<?php
 
 class ParkingsController extends AppController{
  public $uses = array('Station','Tax','Bus','Parking');
  
  public function beforeFilter(){
      parent::beforeFilter();
      $this->Auth->allow('searchParkings', 'searchStation', 'route');
    }

    public function isAuthorized($user){
      if (in_array($this->action, array('edit', 'delete','add','view'))){
        if ($user['type'] != 'admin'){
          return false;
        }
      }
    
      return true;
    }

  public function index() {
  	$this->set('parkings', $this->Parking->find('all'));
	   
  }
  public function view($id = null) {
    $this->Parking->id = $id;
    $this->set('parking', $this->Parking->read());
  }

  public function add() {
    $this->set('stations', $this->Station->find('all',array('conditions'=> array('Station.parking' => 1 ))));
    if($this->request->is('post')){
       if($this->Parking->saveAll($this->request->data)){
         $this->redirect(array('action' => 'index'));
       } 
    }
  }
  public function edit($id = null) {
    $this->set('stations', $this->Station->find('all',array('conditions'=> array('Station.parking' => 1 ))));
    $this->Parking->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Parking->read();
    } 
    else {
      if ($this->Parking->saveAll($this->request->data)) {
          $this->redirect(array('action' => 'index'));
      }
    }
  }
  public function delete($id) {
    if (!$this->request->is('post')) {
    }
    if ($this->Parking->delete($id)) {
      $this->redirect(array('action' => 'index'));
    }
  }

  public function searchStation(){
    $this->set('stations', $this->Station->find('all',array('conditions'=> array('Station.parking' => 1 ))));
    $this->set('filters', 'show');
    if ($this->request->is('post')) {
      $selectedStation = $_POST['Station'];
      $this->redirect(array('action' => 'searchParkings/'.$selectedStation));
    }  
  }

  public function searchParkings($selectedStation){
    $this->set('parkings', $this->Parking->find('all',array('conditions'=> array('Parking.station_acronym' => $selectedStation))));
    
  }
  public function route($string){
    $this->layout = 'mapCar';
    $latlong = explode("_", $string);
    $this->set('latitude', $latlong[0]);
    $this->set('longitude', $latlong[1]);

  }

     		
}

?>